﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace MvcApplication1.Models
{
    public class ViewReportModel
    {
        public string report = "";
        public string VIN = "";
        public int ExcessiveAccelerationCount = 0;
        public int SuddenBrakingCount = 0;
        public int HardTurnCount = 0;
        public int SpeedingCount = 0;
        public double ExcessiveAccelerationTotal = 0;
        public double SuddenBrakingTotal = 0;
        public double HardTurnTotal = 0;
        public double SpeedingTotal = 0;

        public Newtonsoft.Json.Linq.JObject jObject;
        public List<Snapshot> snapshots;

        public ViewReportModel()
        {
            
        }

        /// <summary>
        /// Parses a VIN into a corresponding driver name.
        /// </summary>
        /// <param name="vin">VIN to be parsed.</param>
        /// <param name="username">Username of the customer currently logged in.</param>
        /// <returns>Returns the driver name associated with the VIN, or "Driver Name Not Available" if no driver name is assigned to the given VIN.</returns>
        public string ParseVIN(string vin, string username)
        {
            string driverVIN = vin;
            string line = "";
            string name = "";
            string filePath = HttpContext.Current.Server.MapPath("~/Data/drivernames.txt");
            using (StreamReader reader = new StreamReader(filePath))
            {
                do
                {
                    line = reader.ReadLine();
                } while (line != username && !reader.EndOfStream);

                while (!reader.EndOfStream)
                {
                    line = reader.ReadLine();
                    name = reader.ReadLine();
                    if (line == driverVIN)
                    {
                        return name;
                    }
                }
            }
            return "Driver Name Not Available";
        }
        
        /// <summary>
        /// Finds the total number of speeding instances in a report.  Can be replaced by changing the Summary object in the JSON report.
        /// </summary>
        /// <returns>Total number of speeding instances.</returns>
        public string FindSpeeding()
        {
            int index = 0;
            index = report.IndexOf("Speeding");
            return "0";
        }

        /// <summary>
        /// Finds the total number of excessive acceleration instances in a report.  Can be replaced by changing the Summary object in the JSON report.
        /// </summary>
        /// <returns>Total number of excessive acceleration instances.</returns>
        public string FindExcessiveAcceleration()
        {
            int total = 0;
            int index = 0;
            for (int i = 0; i < 6; i++)
            {
                index = report.IndexOf("LightSpeedingLvl" + (i + 1));
                if (index != -1)
                {
                    string temp = report.Substring(index + 19, 4);
                    string final = "";
                    for (int a = 0; a < temp.Length; a++)
                    {
                        if (Char.IsNumber(temp[a]))
                        {
                            final += temp[a];
                        }
                    }
                    total += Convert.ToInt32(final);
                }
            }
            return "" + total;
        }

        /// <summary>
        /// Finds the total number of sudden braking instances in a report.  Can be replaced by changing the Summary object in the JSON report.
        /// </summary>
        /// <returns>Total number of sudden braking instances.</returns>
        public string FindSuddenBraking()
        {
            int total = 0;
            int index = 0;
            for (int i = 0; i < 6; i++)
            {
                index = report.IndexOf("LightSpeedingLvl" + (i + 1));
                if (index != -1)
                {
                    string temp = report.Substring(index + 19, 4);
                    string final = "";
                    for (int a = 0; a < temp.Length; a++)
                    {
                        if (Char.IsNumber(temp[a]))
                        {
                            final += temp[a];
                        }
                    }
                    total += Convert.ToInt32(final);
                }
            }
            return "" + total;
        }

        /// <summary>
        /// Finds the total number of hard turning instances in a report.  Can be replaced by changing the Summary object in the JSON report.
        /// </summary>
        /// <returns>Total number of hard turning instances.</returns>
        public string FindHardTurning()
        {
            int total = 0;
            int index = 0;
            for (int i = 0; i < 6; i++)
            {
                index = report.IndexOf("LightSpeedingLvl" + (i + 1));
                if (index != -1)
                {
                    string temp = report.Substring(index + 19, 4);
                    string final = "";
                    for (int a = 0; a < temp.Length; a++)
                    {
                        if (Char.IsNumber(temp[a]))
                        {
                            final += temp[a];
                        }
                    }
                    total += Convert.ToInt32(final);
                }
            }
            return "" + total;
        }

    }

    public class Snapshot
    {
        public int Entry { get; set; }
        public double Acceleration { get; set; }
        public double Heading { get; set; }
        public double Latitude { get; set; }
        public double Longitude { get; set; }
        public double Fuel { get; set; }
        public double Speed { get; set; }
        public double SpeedLimit { get; set; }
        public double Throttle { get; set; }
        public double XAccel { get; set; }
        public double YAccel { get; set; }
        public double ZAccel { get; set; }
        public List<Dictionary<string, object>> Flags { get; set; }

        public Snapshot()
        {

        }
    }
}